﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DDHDeepTunnel.Interfaces;
using DDHDeepTunnel.Data;
using NativeDataOperation.Interfaces;
using WebFoundation;
using WebFoundation.Data;

namespace DDHDeepTunnel.Business
{
    internal class Procedure : IProcedure
    {
        private DbManager _queryDbManager;
        private DbManager _writeDbManager;

        public Procedure(DbManager queryDbManager, DbManager writeDbManager, OnlineDataModule module)
        {
            this._queryDbManager = queryDbManager;
            this._writeDbManager = writeDbManager;
        }

        public IEnumerable<T> QueryParamsStoredProcedure<T>(string storedProcedureName,
            List<ProcedureParams> procedureParamses = null)
            where T : class
        {
            try
            {
                var connString = _queryDbManager.ConnectionStr;
                var databaseType = _queryDbManager.DBType;
                if (_queryDbManager != null)
                {
                    _queryDbManager.Dispose();
                }
                _queryDbManager = new DbManager(connString, databaseType);
            }
            catch (Exception ex)
            {
                this.GetType().LogDebug("DBManager connection failed. Error: " + ex.Message);
            }
            return (new SqlProcedureQueryDAO<T>(_queryDbManager)).QueryParamsStoredProcedure<T>(storedProcedureName, procedureParamses);
        }

        public IEnumerable<T> QueryMultipleParamsStoredProcedure<T>(string storedProcedureName, out List<DataBaseSystemPaging> dataBaseSystemPagings,
            List<ProcedureParams> procedureParamses = null)
            where T : class
        {
            try
            {
                var connString = _queryDbManager.ConnectionStr;
                var databaseType = _queryDbManager.DBType;
                if (_queryDbManager != null)
                {
                    _queryDbManager.Dispose();
                }
                _queryDbManager = new DbManager(connString, databaseType);
            }
            catch (Exception ex)
            {
                this.GetType().LogDebug("DBManager connection failed. Error: " + ex.Message);
            }
            return (new SqlProcedureQueryDAO<T>(_queryDbManager)).QueryMultipleParamsStoredProcedure<T>(storedProcedureName, out dataBaseSystemPagings, procedureParamses);
        }
    }
}